<template>
  <keep-alive>
    <div id="category">
      <!-- <div class="box_blank"></div> -->

      <div class="top_search_box"><input class="search_input" @click="toSearch" placeholder="搜索商品名称/编号" /></div>

      <section class="main">
        <div class="section_left">
          <ul>
            <li @click="getGoods(item.id)" v-for="item in category" :class="item.id === categoryActive ? 'on' : ''">
              <a>{{ item.title }}</a>
            </li>
          </ul>
        </div>
        <div class="section_right">
          <ul>
            <li v-for="item in goodsList">
              <div class="goods_box" @click="goGoodsViews(item.id)">
                <div class="left"><img :src="item.image" /></div>
                <div class="left2">
                  <div class="wrap">
                  <div class="title">{{ item.title }}</div>
                  <div class="price">￥{{ item.price }}</div>
                  </div>
                </div>
              </div>
            </li>
          </ul>
          <div class="box_blank2">没有更多了</div>
        </div>
      </section>

      <app-nav></app-nav>
    </div>
  </keep-alive>
</template>

<script>
import nav from './nav.vue';

export default {
  components: {
    'app-nav': nav
  },
  data() {
    return {
      list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      category: [],
      categoryActive: 0,
      goodsList: []
    };
  },
  created() {
    this.getCategory();
  },
  methods: {
    toSearch() {

      this.$router.push('/search');

    },
    getCategory() {
      this.apiPost('/shop.index/category', {}).then(res => {
        this.category = res.data.lists;
        console.log(this.category[0].id);
        this.categoryActive = this.category[0].id;
        this.getGoods(this.categoryActive);
      });
    },
    // 获取指定分类商品
    getGoods(cid) {
      this.apiPost('/shop.index/goods', { cid: cid }).then(res => {
        this.goodsList = res.data.lists;
        console.log(this.goodsList);
        this.categoryActive = cid;
      });
    },
    goGoodsViews(id = 0) {
      this.$router.push({
        path: 'goodsViews',
        query: {
          goods_id: id
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
  @import "@/assets/scss/variable.scss";
.goods_box {
  border: 1px solid #eee;
  display: flex;
  overflow: hidden;
  .left {
    display: flex;
    align-items: center;
    img {

      width: 80px;
      height: 80px;
    }
  }
  .left2 {
    display: flex;
    align-items: center;
    padding-left: 10px;
    .title {
      color: black;
      font-size: 15px;
      overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;

    }
    .price {
      color: $colorAccent;
      font-weight: bold;
    }
  }
}

.box_blank {
  height: 50px;
}
.box_blank2 {
  height: 100px;
  text-align: center;
  color: #bbb;
  font-size: 13px;
}

#category {
  height: 100%;
}
// #category > .title {
//   z-index: 9;
//   position: fixed;
//   width: 100%;
// }

.main {
  overflow: hidden;
  height: calc(100% - 100px);
}

.section_right {
  float: left;
  width: 75%;
  height: 100%;
  background: #fff;
  overflow: auto;
  ul {
    overflow: hidden;
    li {
      width: 100%;
      float: left;
      padding: 7px;
      box-sizing: border-box;
    }
  }
}

.section_left {
  float: left;
  width: 25%;
  height: 100%;
  overflow: auto;
  background: #f7f7f7;
  li {
    width: 100%;
    height: 3rem;
    overflow: hidden;
    border-bottom: 1px solid #eee;

    box-sizing: border-box;
    &.on {
      background: #fff;
      border-right: 0;
    }
    a {
      display: block;
      color: #333;
      font-size: 0.9rem;
      text-align: center;
      height: 3rem;
      line-height: 3rem;
    }
  }
}
</style>
